package com.ruoyi.behindTeam.service.impl;

import com.ruoyi.common.exception.ServiceException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.behindTeam.domain.yyyySysUser;
import com.ruoyi.behindTeam.mapper.YYYDocTeamMapper;
import com.ruoyi.behindTeam.domain.YYYDocTeam;
import com.ruoyi.behindTeam.service.IYYYDocTeamService;

/**
 * 所有团队Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-18
 */
@Service
public class YYYDocTeamServiceImpl implements IYYYDocTeamService 
{
    @Autowired
    private YYYDocTeamMapper yYYDocTeamMapper;

    /**
     * 查询所有团队
     * 
     * @param teamId 所有团队主键
     * @return 所有团队
     */
    @Override
    public YYYDocTeam selectYYYDocTeamByTeamId(Long teamId)
    {
        return yYYDocTeamMapper.selectYYYDocTeamByTeamId(teamId);
    }

    /**
     * 查询所有团队列表
     * 
     * @param yYYDocTeam 所有团队
     * @return 所有团队
     */
    @Override
    public List<YYYDocTeam> selectYYYDocTeamList(YYYDocTeam yYYDocTeam)
    {
        return yYYDocTeamMapper.selectYYYDocTeamList(yYYDocTeam);
    }

    /**
     * 新增所有团队
     * 
     * @param yYYDocTeam 所有团队
     * @return 结果
     */
    @Transactional
    @Override
    public int insertYYYDocTeam(YYYDocTeam yYYDocTeam)
    {
        int rows = yYYDocTeamMapper.insertYYYDocTeam(yYYDocTeam);
        insertSysUser(yYYDocTeam);
        return rows;
    }

    /**
     * 修改所有团队
     * 
     * @param yYYDocTeam 所有团队
     * @return 结果
     */
    @Transactional
    @Override
    public int updateYYYDocTeam(YYYDocTeam yYYDocTeam)
    {
        yYYDocTeamMapper.deleteSysUserByTeamId(yYYDocTeam.getTeamId());
        insertSysUser(yYYDocTeam);
        return yYYDocTeamMapper.updateYYYDocTeam(yYYDocTeam);
    }

    /**
     * 批量删除所有团队
     * 
     * @param teamIds 需要删除的所有团队主键
     * @return 结果
     */
    @Transactional
//    @Override
//    public int deleteYYYDocTeamByTeamIds(Long[] teamIds)
//    {
//        yYYDocTeamMapper.deleteSysUserByTeamIds(teamIds);
//        return yYYDocTeamMapper.deleteYYYDocTeamByTeamIds(teamIds);
//    }
    @Override
    public int deleteYYYDocTeamByTeamIds(Long[] teamIds) {
        // 遍历每个团队ID，检查是否有关联医生
        for (Long teamId : teamIds) {
            // 复用XML中已有的selectSysUserList查询（根据teamId查询关联用户）
            List<yyyySysUser> doctorList = yYYDocTeamMapper.selectSysUserList(teamId);
            // 校验：如果团队中存在医生，则阻止删除（生产环境推荐显式抛异常）
            if (doctorList != null && !doctorList.isEmpty()) {
                throw new ServiceException("团队ID为" + teamId + "中存在医生，无法删除");
            }
        }
        // 先删除团队关联的用户关系，再删除团队本身
        int rows = yYYDocTeamMapper.deleteSysUserByTeamIds(teamIds);
        rows += yYYDocTeamMapper.deleteYYYDocTeamByTeamIds(teamIds);
        return rows;
    }



    /**
     * 删除所有团队信息
     * 
     * @param teamId 所有团队主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYYYDocTeamByTeamId(Long teamId)
    {
        yYYDocTeamMapper.deleteSysUserByTeamId(teamId);
        return yYYDocTeamMapper.deleteYYYDocTeamByTeamId(teamId);
    }

    /**
     * 新增用户信息信息
     * 
     * @param yYYDocTeam 所有团队对象
     */
    public void insertSysUser(YYYDocTeam yYYDocTeam)
    {
        List<yyyySysUser> sysUserList = yYYDocTeam.getSysUserList();
        Long teamId = yYYDocTeam.getTeamId();
        if (StringUtils.isNotNull(sysUserList))
        {
            List<yyyySysUser> list = new ArrayList<yyyySysUser>();
            for (yyyySysUser sysUser : sysUserList)
            {
                sysUser.setTeamId(teamId);
                list.add(sysUser);
            }
            if (list.size() > 0)
            {
                yYYDocTeamMapper.batchSysUser(list);
            }
        }
    }
}
